package com.ease.core.components {
   import com.ease.core.components.ArrowPopUpMenuButton;
   import com.ease.core.events.PerspectiveEvent;
   import com.ease.core.managers.PerspectiveManager;
   
   import flash.events.MouseEvent;
   
   import mx.binding.utils.BindingUtils;
   import mx.containers.HBox;
   import mx.controls.Button;
   import mx.controls.Menu;
   import mx.controls.PopUpButton;
   import mx.controls.PopUpMenuButton;
   import mx.core.ScrollPolicy;
   import mx.events.FlexEvent;
   import mx.events.ItemClickEvent;
   import mx.events.MenuEvent;

   public class PerspectiveBox extends HBox {
      protected var perspectiveSwitcher:PopUpMenuButton = null;
      protected var popupButton:PopUpMenuButton;
      public var perspectiveBar:PerspectiveBar = null;

      [Embed(source="/assets/icon/open_perspective.gif")]
      public var iconOpenPerspectiveClass:Class;
      

      public function PerspectiveBox() {
         super();
         horizontalScrollPolicy = ScrollPolicy.OFF;
         this.styleName = "perspectiveBox";
         this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
      }

      protected function onCreationComplete(event:FlexEvent):void {
      }

      protected function onPerspectiveToggleButtonBarClick(event:ItemClickEvent):void {
      }
      
      protected function onPerspectiveMenuClick(event:MenuEvent):void {
      }

      override protected function createChildren():void {
         super.createChildren();
         if (!perspectiveSwitcher) {
            perspectiveSwitcher = new ArrowPopUpMenuButton();
//            perspectiveSwitcher.setStyle("popUpIcon", iconOpenPerspectiveClass);
			perspectiveSwitcher.setStyle("icon", iconOpenPerspectiveClass);
            perspectiveSwitcher.addEventListener(MenuEvent.ITEM_CLICK, onPerspectiveMenuClick);
         }
         this.addChild(perspectiveSwitcher);

         if (!perspectiveBar) {
            perspectiveBar = new PerspectiveBar();
            perspectiveBar.addEventListener(ItemClickEvent.ITEM_CLICK, onPerspectiveToggleButtonBarClick);
         }
         this.addChild(perspectiveBar);
         
         if (null == popupButton) {
            popupButton = new PopUpMenuButton();
            popupButton.width = 27;
            popupButton.setStyle("paddingTop", 5);
            popupButton.setStyle("paddingRight", 0);
            popupButton.height = 18;
            popupButton.openAlways = true;
            popupButton.styleName = "viewTabBarPopUpButton";
            popupButton.setStyle("arrowButtonWidth", 0);
            popupButton.addEventListener(MenuEvent.ITEM_CLICK, onPerspectiveMenuClick);
            popupButton.addEventListener(MouseEvent.CLICK, onPopupButtonClick);
            
            rawChildren.addChild(popupButton);
         }
      }
      
      private function onPopupButtonClick(e:MouseEvent):void {
         e.stopImmediatePropagation();
      }

      protected function onPerspectiveOpened(event:PerspectiveEvent):void {
      }
      
      protected function invalidateDisplayTabs():void {
         var gap:int = (this.height - popupButton.height) / 2;
         var popupButtonX:int = gap;
         
         var restUnscaledWidth:int = width - perspectiveSwitcher.width - popupButton.width; //[while parent is canvas in ViewWindow
         //popupButton.move(unscaledWidth + gap, 0);
         //popupButton.height = height;
         var hiddenCount:int = 0;
         for (var i:int = 0; i < perspectiveBar.numChildren; i++) {
            var currTab:Button = Button(perspectiveBar.getChildAt(i));
            if ((currTab.x + currTab.width) >= restUnscaledWidth) {
               currTab.visible = false;
               popupButton.visible = true;
               popupButton.enabled = true;
               hiddenCount++;
            }
            else {
               currTab.visible = true;
               popupButton.close();
               popupButton.visible = false;
               popupButton.enabled = false;
            }
         }
         if (popupButton.visible) {
            popupButton.move(width - popupButton.width, gap);
            popupButton.label = String(hiddenCount);
         }
      }
      
      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
         super.updateDisplayList(unscaledWidth, unscaledHeight);
         invalidateDisplayTabs();
      }
   }
}